/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Graphic;


import businessLogic.PlayerActions;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JPanel;


//classe CursorOverHands

public class CursorOverHands extends JPanel implements Serializable{
    /**
	 *
	 */
	private static final long serialVersionUID = 1L;

   

    private Rectangle2D.Double rect1 = new Rectangle2D.Double(10,10,120,100);
    private Line2D.Double line = new Line2D.Double(40, 40, 360, 360);
    Image img1 = Toolkit.getDefaultToolkit().getImage("src/carte/asso_cuori.png");
    Image img2 = Toolkit.getDefaultToolkit().getImage("src/carte/asso_fiori.png");
    Image img3 = Toolkit.getDefaultToolkit().getImage("src/carte/asso_quadri.png");
    Image img4 = Toolkit.getDefaultToolkit().getImage("src/carte/asso_picche.png");
    ArrayList<PositionArea> vettoreArea=new ArrayList<PositionArea>();
    ArrayList<Image> vettoreImage=new ArrayList<Image>();
    ArrayList<Box> box=new ArrayList<Box>();
    int boxCardSetVisible;
    PlayerActions playerActions;
    private static int cardPosition=0;
    //private DraftLayout draft=new DraftLayout();

    public Image getImg1() {
        return img1;
    }

    public void setImg1(Image img1) {
        this.img1 = img1;
    }


    public CursorOverHands(){

        

        setBackground(Color.decode("#F5FFFA"));

       boxCardSetVisible=0;

       initBox();

        //aggiunta mouseListener evento: Pressione del mouse
        addMouseListener(new MouseAdapter(){
            @Override
             public void mousePressed(MouseEvent e) {
               //System.out.println("Click in Area 1");
               System.out.println("Click X: "+e.getX());
               System.out.println("Click Y: "+e.getY());
               System.out.println();

               //recupera area inteessata;
               int clickedBox=mergeBox(e.getX(),e.getY());

               if((clickedBox!=-1) && (clickedBox<=boxCardSetVisible) ){
                   //Box cliccato è visibile
                   System.out.println("Area Visibile Cliccata: "+clickedBox);

                   //fai controlli per sistemare la carta
                   System.out.println("Card Position: "+cardPosition);
                   todo(cardPosition,clickedBox);

                   
                  
                  


               }
               else{
                   System.out.println("Campo Vuoto ");
               }

             }
        });
    }
    

    //Disegno Componenti
    protected void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            //disegno immagine
              for(int i=0;i<vettoreArea.size();i++){
//                  System.out.println("Dim Area: "+vettoreArea.size());
                  for(int j=0;j<vettoreArea.get(i).size();j++){
                      if(vettoreImage.get(j)!=null){
                          g2d.drawImage(vettoreImage.get(j),vettoreArea.get(i).getPositionArea().get(j).getX(),
                               vettoreArea.get(i).getPositionArea().get(j).getY(), this);
                      }
                  }
                      
            
//                  System.out.println("Dim Area interna: "+vettoreArea.get(i).size());
              }
    }


    protected void DrawCard(int x, int y, ArrayList<PositionArea> vettore, ArrayList<Image> vettoreImage){
      this.vettoreArea=vettore;
      this.vettoreImage=vettoreImage;
      repaint();
    }


    public void updateBoxOccupied(int number){
        boxCardSetVisible=number;
    }


    public void removeBoxOccupied(int number){
        boxCardSetVisible=number;
    }

    public int mergeBox(int x,int y){

        for(int i=0;i<box.size();i++){

//            System.out.println();
//            System.out.println("RIGA "+i);
//            System.out.println();

            int minX=box.get(i).getBox().get(0).getX();
//            System.out.println("minX "+minX);
            int maxX=box.get(i).getBox().get(1).getX();
//            System.out.println("maxX "+maxX);
            int minY=box.get(i).getBox().get(0).getY();
//            System.out.println("minY "+minY);
            int maxY=box.get(i).getBox().get(3).getY();
//            System.out.println("maxY "+maxY);
//            System.out.println();

            if( ((minX <= x) && (x<=maxX)) && ((minY <= y) && (y <= maxY))){
                //siamo nel Box Cliccato
//                System.out.println("MATCH riga "+i);
//                System.out.println();
                return i;
                
            }
        }
//        System.out.println("NO MATCH");
//        System.out.println();
        return -1;

    }


    public void initBox(){
        //1 box
        Box b1=new Box(new Punti(10,20),new Punti(154,20),new Punti(10,110),new Punti(154,110));
        //2 box
        Box b2=new Box(new Punti(176,20),new Punti(320,20),new Punti(176,110),new Punti(320,110));
        //3 box
        Box b3=new Box(new Punti(340,20),new Punti(485,20),new Punti(340,110),new Punti(485,110));
        //4 box
        Box b4=new Box(new Punti(506,20),new Punti(650,20),new Punti(506,110),new Punti(650,110));
        //5 box
        Box b5=new Box(new Punti(671,20),new Punti(815,20),new Punti(671,110),new Punti(815,110));
        //6 box
        Box b6=new Box(new Punti(10,147),new Punti(154,147),new Punti(10,235),new Punti(154,235));
        //7 box
        Box b7=new Box(new Punti(176,147),new Punti(320,147),new Punti(176,235),new Punti(320,235));
        //8 box
        Box b8=new Box(new Punti(340,147),new Punti(485,147),new Punti(340,235),new Punti(485,235));
        //9 box
        Box b9=new Box(new Punti(506,147),new Punti(650,147),new Punti(506,235),new Punti(650,235));
        //10 box
        Box b10=new Box(new Punti(671,147),new Punti(815,147),new Punti(671,235),new Punti(815,235));
        //11 box
        Box b11=new Box(new Punti(10,277),new Punti(154,277),new Punti(10,366),new Punti(154,366));
        //12 box
        Box b12=new Box(new Punti(176,277),new Punti(320,277),new Punti(176,366),new Punti(320,366));
        //13 box
        Box b13=new Box(new Punti(340,277),new Punti(485,277),new Punti(340,366),new Punti(485,366));
        //14 box
        Box b14=new Box(new Punti(506,277),new Punti(650,277),new Punti(506,366),new Punti(650,366));
        //15 box
        Box b15=new Box(new Punti(671,277),new Punti(815,277),new Punti(671,366),new Punti(815,366));
        //16 box
        Box b16=new Box(new Punti(10,408),new Punti(154,408),new Punti(10,496),new Punti(154,496));
        //17 box
        Box b17=new Box(new Punti(176,408),new Punti(320,408),new Punti(176,496),new Punti(320,496));
        //18 box
        Box b18=new Box(new Punti(340,408),new Punti(485,408),new Punti(340,496),new Punti(485,496));
        //19 box
        Box b19=new Box(new Punti(506,408),new Punti(650,408),new Punti(506,496),new Punti(650,496));
        //20 box
        Box b20=new Box(new Punti(671,408),new Punti(815,408),new Punti(671,496),new Punti(815,496));

        box.add(b1);box.add(b2);box.add(b3);box.add(b4);box.add(b5);
        box.add(b6);box.add(b7);box.add(b8);box.add(b9);box.add(b10);
        box.add(b11);box.add(b12);box.add(b13);box.add(b14);box.add(b15);
        box.add(b16);box.add(b17);box.add(b18);box.add(b19);box.add(b20);

    }

    public void setCardPosition(int p, PlayerActions player){
        cardPosition=p;
        playerActions=player;
    }


    public void todo(int cardPos,int clickbox){
        playerActions.tryToAddOrRepleaceCard(cardPos, clickbox);
        playerActions.drawCardsOnTheTable();

    }
    
    
}
